package com.sky.zzh.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.sky.widget.material.switchbutton.MaterialSwitchButton;
import com.sky.zzh.BaseFragment;
import com.sky.zzh.R;
import com.sky.zzh.util.UserInfoManager;
/**
 * 设置密码锁
 * @author skypan
 *
 */
public class SetPwdLockFragment extends BaseFragment {

	private TextView mTvChangeLock,mTvLockStatus;
	private MaterialSwitchButton mSbLock;
	private ImageView mBtnBack;
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mTvChangeLock = (TextView) view.findViewById(R.id.tv_change_lock);
		mTvLockStatus = (TextView) view.findViewById(R.id.tv_lock_status);
		mSbLock = (MaterialSwitchButton) view.findViewById(R.id.sb_lock);
		mBtnBack = (ImageView) view.findViewById(R.id.btn_back);
		setListeners();
		if(UserInfoManager.getInstance().isLock()){
			mSbLock.setChecked(true);
			mTvLockStatus.setText("锁屏密码已打开");
			mTvChangeLock.setTextColor(getActivity().getResources().getColor(R.color.color_black));
			mTvChangeLock.setClickable(true);
		}else{
			mSbLock.setChecked(false);
			mTvLockStatus.setText("锁屏密码已关闭");
			mTvChangeLock.setTextColor(getActivity().getResources().getColor(R.color.color_gray_light));
			mTvChangeLock.setClickable(false);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_lock, container, false);
		return view;
	}
	
	private void setListeners(){
		mBtnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getActivity().finish();
			}
		});
		
		mSbLock.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean checked) {
				// TODO Auto-generated method stub
				if(checked){
					UserInfoManager.getInstance().setLock(true);
					UserInfoManager.getInstance().sync(getActivity(), true);
					mTvLockStatus.setText("锁屏密码已打开");
					mTvChangeLock.setTextColor(getActivity().getResources().getColor(R.color.color_black));
					mTvChangeLock.setClickable(true);
					mTvChangeLock.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(getActivity(),SetAppPwdActivity.class);
							startActivity(intent);
						}
					});
				}else{
					UserInfoManager.getInstance().setLock(false);
					UserInfoManager.getInstance().sync(getActivity(), true);
					mTvLockStatus.setText("锁屏密码已关闭");
					mTvChangeLock.setTextColor(getActivity().getResources().getColor(R.color.color_gray_light));
					mTvChangeLock.setClickable(false);
				}
			}
		});
	}

}
